//
//  BBRawVideoView.m
//  BBTouch
//
//  Created by ben smith on 6/13/08.
//  This file is part of BBTouch.
//
//  BBTouch is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  BBTouch is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.

//  You should have received a copy of the GNU Lesser General Public License
//  along with BBTouch.  If not, see <http://www.gnu.org/licenses/>.
// 
//  Copyright 2008 Ben Britten Smith ben@benbritten.com .
//

#import "BBRawVideoView.h"
#import "BBConfigurationController.h"

@implementation BBRawVideoView

-(void)awakeFromNib
{
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imageUpdate:) name:@"BBRawVideoImageChange" object:nil];
}

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
			[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imageUpdate:) name:@"BBRawVideoImageChange" object:nil];
    }
    return self;
}

-(void)drawRect:(NSRect)rect
{
	[[NSColor darkGrayColor] set];
	NSRectFill(rect);
	
	if ([[NSUserDefaults standardUserDefaults] boolForKey:@"BBShowRawVideoFlag"]) {
		[self drawRawVideo];
	}
}

-(void)drawRawVideo
{
	// draw the raw video image
	// NSImages are flipped in the vertical, so we need to flip it back to be
	// inline with the rest of the coordinates
	// this could probably be done with a single transform, but keeping it simple for now
	NSAffineTransform * scale = [NSAffineTransform transform];
	NSAffineTransform * translate = [NSAffineTransform transform];
	[scale scaleXBy:1.0 yBy:-1.0];
	[translate translateXBy:0.0 yBy:[image size].height];
	
	// concat these transforms to the current context
	[translate concat];
	[scale concat];
	// draw the image
	[image drawAtPoint:NSMakePoint(0,0) 
						fromRect:NSZeroRect
					 operation:NSCompositeSourceOver
						fraction:1.0]; 	
	// undo the tranforms, dont want them to apply to any other drawing
	[scale invert];
	[scale concat];
	[translate invert];
	[translate concat];
}

-(void)imageUpdate:(id)notification
{
	if ([[notification object] isKindOfClass:[NSImage class]]) {
		[self setValue:[notification object] forKey:@"image"];
		// TODO: really only need to do this when the size changes, which really should be never (ie just once)
		NSSize scaleFactor = [[BBConfigurationController sharedConfigurationController] rawImageSize];
		scaleFactor.width = [self bounds].size.width/scaleFactor.width;
		scaleFactor.height = [self bounds].size.height/scaleFactor.height;
		[self scaleUnitSquareToSize:scaleFactor];
		
		[self setNeedsDisplay:YES];
	}
}

@end
